﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.WinUI
{
    public partial class FrmSelectStocks : Form
    {
        public FrmSelectStocks()
        {            
            InitializeComponent();         
        }

        private void FrmSelectStocks_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            List<string> trades = ClientCache.Instance.AllTrades;
            foreach (string trade in trades)
            {
                this.cbTrades.Items.Add(trade);
            }
            this.cbTrades.SelectedIndex = 0;
        }

        private void cbTrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.flplStocks.Controls.Clear();
            List<Stock> stocks = ClientCache.Instance.GetStocksByTrade((string)this.cbTrades.SelectedItem);
            foreach (Stock stock in stocks)
            {
                Button btn = new Button();
                btn.FlatStyle = FlatStyle.Flat;
                btn.Width = 130;
                btn.Cursor = Cursors.Hand;
                btn.BackColor = Color.Black;
                btn.ForeColor = Color.Coral;
                btn.Text = stock.ToShowString();
                btn.Tag = stock;
                btn.Click += new EventHandler(btn_Click);
                this.flplStocks.Controls.Add(btn);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            OnStockSelected((Stock)(((Button)sender).Tag));
        }

        public delegate void StockSelectedHandler(Stock stock);

        public event StockSelectedHandler StockSelected;

        public void OnStockSelected(Stock stock)
        {
            if (StockSelected != null)
            {
                StockSelected(stock);
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            this.Opacity = (double)this.trackBar1.Value / (double)100;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
